﻿
namespace Hidistro.UI.Common.Controls
{
    using Hidistro.Entities;
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml;
    public class MonthlyIncomeSelector : WebControl
    {
        private int? currentMI;
        private bool dataLoaded;
        private WebControl ddlMonthlyIncome;
        private int? MIId;

        public MonthlyIncomeSelector()
        {
            this.MITitle = "";
            this.NullToDisplay = "其他";
            this.Separator = "，";

        }

        protected override void CreateChildControls()
        {
            this.Controls.Clear();
            if (!this.dataLoaded)
            {
                if (!string.IsNullOrEmpty(this.Context.Request.Form["MonthlyIncomeValue"]))
                {
                    this.currentMI = new int?(int.Parse(this.Context.Request.Form["MonthlyIncomeValue"]));
                }
                this.dataLoaded = true;
            }
            if (this.currentMI.HasValue)
            {
                this.MIId = new int?(this.currentMI.Value);
            }
            this.Controls.Add(CreateTitleControl(this.MITitle));
            this.ddlMonthlyIncome = this.CreateDropDownList("ddlMonthlyIncome");
            FillDropDownList(this.ddlMonthlyIncome, RegionHelper.GetMonthlyIncome(), this.MIId);
            this.Controls.Add(CreateTag("<span>"));
            this.Controls.Add(this.ddlMonthlyIncome);
            this.Controls.Add(CreateTag("</span>"));
        }

        private WebControl CreateDropDownList(string controlId)
        {
            WebControl control = new WebControl(HtmlTextWriterTag.Select);
            control.Attributes.Add("id", controlId);
            control.Attributes.Add("name", controlId);
            control.Attributes.Add("selectset", "MI");
            WebControl child = new WebControl(HtmlTextWriterTag.Option);
            child.Controls.Add(new LiteralControl(this.NullToDisplay));
            child.Attributes.Add("value", "");
            control.Controls.Add(child);
            return control;
        }

        private static WebControl CreateOption(string val, string text)
        {
            WebControl control = new WebControl(HtmlTextWriterTag.Option);
            control.Attributes.Add("value", val);
            control.Controls.Add(new LiteralControl(text.Trim()));
            return control;
        }

        private static Literal CreateTag(string tagName)
        {
            return new Literal { Text = tagName };
        }

        private static Label CreateTitleControl(string title)
        {
            Label label = new Label
            {
                Text = title
            };
            label.Attributes.Add("style", "margin-left:5px");
            return label;
        }

        private static void FillDropDownList(WebControl ddlMonthlyIncome, Dictionary<int, string> regions, int? selectedId)
        {
            foreach (int num in regions.Keys)
            {
                WebControl child = CreateOption(num.ToString(CultureInfo.InvariantCulture), regions[num]);
                if (selectedId.HasValue && (num == selectedId.Value))
                {
                    child.Attributes.Add("selected", "true");
                }
                ddlMonthlyIncome.Controls.Add(child);
            }
        }

        public int? GetSelectedMI()
        {
            if (!string.IsNullOrEmpty(this.Context.Request.Form["MonthlyIncomeValue"]))
            {
                return new int?(int.Parse(this.Context.Request.Form["MonthlyIncomeValue"]));
            }
            return null;
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);
            writer.AddAttribute("id", "MonthlyIncomeValue");
            writer.AddAttribute("name", "MonthlyIncomeValue");
            writer.AddAttribute("value", this.currentMI.HasValue ? this.currentMI.Value.ToString(CultureInfo.InvariantCulture) : "");
            writer.AddAttribute("type", "hidden");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            writer.AddAttribute("id", "MonthlyIncomeNull");
            writer.AddAttribute("name", "MonthlyIncomeNull");
            writer.AddAttribute("value", this.NullToDisplay);
            writer.AddAttribute("type", "hidden");
            writer.RenderBeginTag(HtmlTextWriterTag.Input);
            writer.RenderEndTag();
            //string script = string.Format("<script src=\"{0}\" type=\"text/javascript\"></script>", this.Page.ClientScript.GetWebResourceUrl(base.GetType(), "Hidistro.UI.Common.Controls.monthlyincome.helper.js"));
            //writer.Write(script);
        }

        public void SetSelectedMI(int? selectedMI)
        {
            this.currentMI = selectedMI;
            this.dataLoaded = true;
        }


        public override ControlCollection Controls
        {
            get
            {
                base.EnsureChildControls();
                return base.Controls;
            }
        }


        public string NullToDisplay { get; set; }

        public string MITitle { get; set; }

        public string Separator { get; set; }
    }
}
